package kos.robert.loader;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.CountDownLatch;

import javax.swing.*;
import kos.robert.animation.*;
import kos.robert.core.Core;
import kos.robert.core.KeyTest;
import kos.robert.screens.ScreenManager;

public class GravityLoader extends Core implements KeyListener {

	public static void main(String[] args) {
		new GravityLoader().run();
	}

	private ArrayList<SimpleObject> Layer;
	private boolean generate;
	private Random random = new Random();
	private int count;
	private int maxCount = 200;
	private float velocityLost = 0.7f;

	@Override
	public void init() {
		super.init();
		Window w = screen.getFullScreenWindow();
		w.addKeyListener(this);
		Layer = new ArrayList<>();
		SimpleObject so = new SimpleObject(0, 100, 10, 10);
		so.setVelocityX(0.1f);
		// so.setVelocityY(0.1f);
		Layer.add(so);
	}

	// update animation
	@Override
	public synchronized void update(long timePassed) {
		if (generate && (count++ % 2 == 0))
			addNewSimpleObject();
		for (SimpleObject object : Layer) {
			object.update(timePassed);
			if (object.getX() < 0) {
				object.setVelocityX(velocityLost * Math.abs(object.getVelocityX()));
				object.setX(-object.getX());
			} else if (object.getX() + object.getWidth() >= screen.getWidth()) {
				object.setVelocityX(velocityLost * -Math.abs(object.getVelocityX()));
				object.setX(object.getX() - (object.getX() + object.getWidth() - screen.getWidth()));
			}

			if (object.getY() < 0) {
				object.setVelocityY(velocityLost * Math.abs(object.getVelocityY()));
				object.setY(-object.getY());
			} else if (object.getY() + object.getHeight() >= screen.getHeight()) {
				object.setVelocityY(velocityLost * -Math.abs(object.getVelocityY()));
				object.setY(object.getY() - (object.getY() + object.getHeight() - screen.getHeight()));
			}
		}
	}

	@Override
	public synchronized void draw(Graphics2D g) {
		Window w = screen.getFullScreenWindow();
		g.setColor(w.getBackground());
		g.fillRect(0, 0, screen.getWidth(), screen.getHeight());
		g.setColor(w.getForeground());
		for (SimpleObject object : Layer) {
			int xpos = Math.round(object.getX());
			int ypos = Math.round(object.getY());
			g.setColor(Color.GREEN);
			g.fillOval(xpos, ypos, object.getWidth(), object.getHeight());
			g.setColor(Color.RED);
			g.fillOval(xpos + 1, ypos + 1, object.getWidth() - 2, object.getHeight() - 2);
		}
	}

	public synchronized void addNewSimpleObject() {
		SimpleObject so = new SimpleObject(0, 100, 10, 10);
		so.setVelocityX(5 * random.nextFloat() / 8 + 0.2f);
		Layer.add(so);
		// System.out.println("TOTAL NUMBER OF OBJECTS: " + Layer.size());
		for (int i = Layer.size() - maxCount; i > 0; i--) {
			Layer.remove(0);
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
			stop();
		} else {
			generate = generate ? false : true;
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// Nothing to do
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// Nothing to do
	}
}